CC=gcc

ROOT=.
PROJDIR=$(ROOT)/../..
OBJDIR=$(PROJDIR)/obj
SRCDIR=$(ROOT)


ASSRCS=$(wildcard $(SRCDIR)/*.S)
astmps=$(notdir $(ASSRCS))
ASTMPS=$(patsubst %.S,$(SRCDIR)/%.s,$(astmps))
ASOBJS=$(patsubst %.s,$(OBJDIR)/%.o,$(ASTMPS))

ASFLAGS=--64

ifeq ($(DEBUG), ON)
ASFLAGS+=-g
endif


SRCS=$(wildcard $(SRCDIR)/*.c)
tmps=$(notdir $(SRCS))
OBJS=$(patsubst %.c,$(ROOT)/%.o,$(tmps))
TARGET=$(OBJDIR)/libintr.a

INCS=-I$(PROJDIR)/include -I$(PROJDIR)/include/driver
CFLAGS=$(INCS) -Wall -Wextra -Werror -Wno-unused-function -mcmodel=large -fno-builtin -m64 -mgeneral-regs-only -nostdlib -nolibc -nostdinc -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding

ifeq ($(DEBUG),ON)
CFLAGS+=-g3
endif

.PHONY: all clean 

all:$(TARGET)

test:
	echo $(SRCS)

$(TARGET):$(OBJS) $(ASOBJS)
	ar rc $@ $^

$(ASOBJS):$(ASTMPS)
	as $(ASFLAGS)  $< -o $@

$(ASTMPS):$(ASSRCS)
	gcc -E $(INCS) -o $@ $<

%.o:%.c
	gcc -c $(CFLAGS) -o $@ $<

clean:
	rm -f $(OBJS) $(TARGET) $(ASOBJS) $(ASTMPS)
